<!--
    Poems of Anne Sexton

    DOM specific functionswidget

    @author    Michel Corne
    @copyright 2013 Michel Corne
    @license   http://www.opensource.org/licenses/gpl-3.0.html GNU GPL v3
-->

<script type="text/javascript">

var sbBrowserLang;

function displayElt(id)
{
    var 
    element;
    
    if (element = getElement(id)) {
        element.style.display = element.tagName.toLowerCase() == 'div'? 'block' : 'inline';
    }    
}

function displayORhideElt(id)
{
    var 
    element;
    
    if (element = getElement(id)) {
        if (element.style.display == 'none') {
            element.style.display = element.tagName.toLowerCase() == 'div'? 'block' : 'inline';
        } else {
            element.style.display = 'none';
        }
    }    
}

function getElement(id, doc)
{
    // gets an element by its ID
    // id: the element ID or (assumed to be) the element object itself (passthru)
    // doc: the document, defaults to the current document
    // returns the element object
    doc || (doc = document);
    return typeof id == 'string'? doc.getElementById(id) : id;
}

function getElements(name)
{
    // gets all the DOM object of a given tag name
    // name: the name of the tag
    // returns an array of all the DOM objects of the given tag
    return document.getElementsByTagName(name);
}

function getBrowserLang()
{   
    (sbBrowserLang = navigator.browserLanguage) || 
    (sbBrowserLang = navigator.language) || 
    (sbBrowserLang = 'en-us');
}

function getSelectValue(id)
{
    // gets the value of the select option currently selected
    // id: the select ID or the select object itself
    // returns the value of the selected option
    // note: select.value is not relialable with IE for values including digits 
    // as they are not returned correctly!
    var element;
    
    return (element = getElement(id))? element.options[element.selectedIndex].value : null;
}

function hideElt(id)
{
    var 
    element;
    
    if (element = getElement(id)) {
        element.style.display = 'none';
    }    
}


function isChrome()
{
    return navigator.userAgent.toLowerCase().indexOf('chrome') > -1;   
}

function processSelect(id, init)
{
    var 
    selected;
        
    if (init) {
        selected = getcookie(id);
        setSelectValue(id, selected);
    } else {
        selected = getSelectValue(id);
        setcookie(id, selected);
    }
    
    return selected;
}

function setSelectValue(id, selected)
{
    var 
    i,
    element;
       
    if (element = getElement(id)) {
        
        for (i = 0; i < element.options.length; i++) {
            if (element.options[i].value == selected) {
                // this is the selected option, sets the select element
                element.options[i].selected = true;
                break;
            }
        }
    }
}

function updateCssAttr(style, rule, attribute, value)
{
    var 
    element,
    i,
    rules,
    sheet;
    
    if (element = getElement(style)) {
        ((sheet = element.styleSheet) || (sheet = element.sheet)) &&  // IE internal styles or others
        ((rules = sheet.rules) || (rules = sheet.cssRules)); // IE external styles or others
        
        if (rules){
            for (i = 0; i < rules.length; i++) {
                if (rules[i].selectorText.toLowerCase() == rule) {
                    rules[i].style[attribute] = value;
                    break;
                }
            }
        }
    }
}

</script>